// This file is part of OpenTSDB.
// Copyright (C) 2017 TeleDB Development Group.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or (at your
// option) any later version.  This program is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
// General Public License for more details.  You should have received a copy
// of the GNU Lesser General Public License along with this program.  If not,
// see <http://www.gnu.org/licenses/>.
package net.opentsdb.tsd.points;

import java.util.Map;
import java.util.Optional;

/**
 * 写入的数据集
 */
public class PutDataContext {
    private PutDataContext(){}

    private String metric;
    private long timestamp;
    private byte[] value;
    private Map<String,String> tags;
    private short flags;
    private byte[] row;
    private byte[] qualifier;
    private String forward;

    public static PutDataContext.Builder newBuilder(String metric,
                                             Map<String,String> tags,
                                             long timestamp,
                                             byte[] value){
        return new PutDataContext.Builder(metric, tags, timestamp, value);
    }

    public static PutDataContext.Builder loadBuilder(PutDataContext context){
        Builder builder = newBuilder(context.getMetric(),
                context.getTags(),
                context.getTimestamp(),
                context.getValue());

        return builder.flags(context.getFlags())
                .row(context.getRow())
                .qualifier(context.getQualifier())
                .forward(context.getForward());
    }

    public static final class Builder {
        private String metric;
        private long timestamp;
        private byte[] value;
        private Map<String,String> tags;
        private short flags;
        private byte[] row;
        private byte[] qualifier;
        private String forward;

        public Builder(String metric, Map<String,String> tags, long timestamp, byte[] value){
            this.metric = metric;
            this.tags = tags;
            this.timestamp = timestamp;
            this.value = value;
        }

        public PutDataContext build(){
            PutDataContext context = new PutDataContext();
            context.metric = this.metric;
            context.timestamp = this.timestamp;
            context.tags = this.tags;
            context.value = this.value;
            context.row = this.row;
            context.flags = this.flags;
            context.qualifier = this.qualifier;
            context.forward = this.forward;

            return context;
        }

        /**
         * 检查参数
         * @return
         */
        public Builder validated(){
            return this;
        }

        public Builder metric(String metric){
            this.metric = metric;
            return this;
        }

        public Builder timestamp(long timestamp){
            this.timestamp = timestamp;
            return this;
        }

        public Builder value(byte[] value){
            this.value = value;
            return this;
        }

        public Builder tags(Map<String,String> tags){
            this.tags = tags;
            return this;
        }

        public Builder flags(short flags){
            this.flags = flags;
            return this;
        }

        public Builder row(byte[] row){
            this.row = row;
            return this;
        }

        public Builder qualifier(byte[] qualifier){
            this.qualifier = qualifier;
            return this;
        }

        public Builder forward(String forward){
            this.forward = forward;
            return this;
        }
    }

    public String getMetric() {
        return metric;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public byte[] getValue() {
        return value;
    }

    public Map<String, String> getTags() {
        return tags;
    }

    public short getFlags() {
        return flags;
    }

    public byte[] getRow() {
        return row;
    }

    public byte[] getQualifier() {
        return qualifier;
    }

    public String getForward() {
        return forward;
    }
}
